package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Category;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping(value = "add_category",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"当前用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iCategoryService.addCategory(categoryName,parentId);
        }
    }

    @RequestMapping(value = "update_category_name",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"当前用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iCategoryService.updateCategoryName(categoryId,categoryName);
        }
    }

    @RequestMapping(value = "seeCategoryList",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<Category>> seeCategoryList(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"当前用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iCategoryService.seeCategoryList(categoryId);
        }
    }

    @RequestMapping(value = "seeDeepCategoryList",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse seeDeepCategoryList(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"当前用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iCategoryService.seeDeepCategoryList(categoryId);
        }
    }
}
